package com.example.springbootqrdemo;

import java.awt.image.BufferedImage;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;
import java.util.zip.ZipEntry;
import java.util.zip.ZipOutputStream;

import javax.imageio.ImageIO;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.hutool.extra.qrcode.QrCodeUtil;

/**
 * Created on 2021-05-31
 */
@RestController
public class HelloController {


    @GetMapping("/creatQr")
    public void createQr(HttpServletRequest request, HttpServletResponse response) throws Exception {

        // 生成指定url对应的二维码到文件，宽和高都是300像素
        BufferedImage qr1 = QrCodeUtil.generate("https://hutool.cn/2", 300, 300);
        BufferedImage qr2 = QrCodeUtil.generate("https://hutool.cn/3", 300, 300);



        String zipName = URLEncoder.encode("制卡流程信息","UTF-8") + String.format("-%s.zip", LocalDate.now().toString());
        response.setContentType("APPLICATION/OCTET-STREAM");
        response.setHeader("Content-Disposition", "attachment; filename=" + zipName);

        ZipOutputStream out = new ZipOutputStream(response.getOutputStream());
        List<String> photoNameList = new ArrayList<>();
        List<InputStream> photoStreamList = new ArrayList<>();




        try {
            photoNameList.add(UUID.randomUUID().toString().substring(0, 8)+".jpg");
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(qr1, "jpg", os);
            InputStream input = new ByteArrayInputStream(os.toByteArray());

            //ok
//            File  file =new File("/Users/cbeann/Desktop/del/qrcode-create.jpg");
//            if (file.exists()){
//                file.delete();
//            }
//            if (!file.exists()){
//                file.createNewFile();
//            }
//            FileOutputStream fileInputStream = new FileOutputStream(file);
//            fileInputStream.write(os.toByteArray());



            photoStreamList.add(input);
        } catch (IOException e) {
        }


        try {
            photoNameList.add(UUID.randomUUID().toString().substring(0, 8)+".jpg");
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            ImageIO.write(qr2, "jpg", os);
            InputStream input = new ByteArrayInputStream(os.toByteArray());

            //ok
            //            File  file =new File("/Users/cbeann/Desktop/del/qrcode-create.jpg");
            //            if (file.exists()){
            //                file.delete();
            //            }
            //            if (!file.exists()){
            //                file.createNewFile();
            //            }
            //            FileOutputStream fileInputStream = new FileOutputStream(file);
            //            fileInputStream.write(os.toByteArray());



            photoStreamList.add(input);
        } catch (IOException e) {
        }





        // 生成指定url对应的二维码到文件，宽和高都是300像素
        //QrCodeUtil.generate("https://hutool.cn/", 300, 300, FileUtil.file("/Users/cbeann/Desktop/del/qrcode.jpg"));

        zip(photoStreamList, photoNameList, out);

        out.close();




    }


    public void zip(List<InputStream> inputStreamList, List<String> fileNameList, ZipOutputStream zipOutStream) {
        for (int i = 0; i < inputStreamList.size(); i++) {
            zip(inputStreamList.get(i), zipOutStream, fileNameList.get(i));
        }
    }

    public void zip(InputStream in, ZipOutputStream out, String fileName) {
        byte[] buffer = new byte[1024];
        int len;
        try {
            out.putNextEntry(new ZipEntry(fileName));
            while ((len = in.read(buffer)) > 0) {
                out.write(buffer, 0, len);
            }
        } catch (IOException e) {
            throw new RuntimeException("zip error");
        } finally {
            try {
                out.flush();
                out.closeEntry();
                in.close();
            } catch (IOException e) {
            }
        }
    }


}
